from django.db import models

# Create your models here.
from common.common_models import CommonInfo
from utils.json_utils import json_loads


class ApiSuite(CommonInfo):
    class Meta:
        verbose_name = "接口分类"
        db_table = "api_suite"

    name = models.CharField("接口分类名称", null=False, max_length=100)
    project_id = models.BigIntegerField("项目id", null=False)
    remark = models.CharField("备注", max_length=255, null=True, blank=True)


class ApiInfo(CommonInfo):
    class Meta:
        verbose_name = "接口信息"
        db_table = "api_info"

    project_id = models.BigIntegerField("项目id", null=False, db_index=True)
    env_id = models.IntegerField("环境id", default=0)
    name = models.CharField("接口名称", null=False, max_length=100, db_index=True)
    method = models.CharField("请求方式", null=True, max_length=20)
    path = models.CharField("请求路径", null=True, blank=True, max_length=255, db_index=True)
    domain = models.CharField("域名", null=True, max_length=50)
    api_suite_id = models.BigIntegerField("接口分类 ID", null=False, db_index=True)
    request_header = models.TextField("请求头", null=True)
    request_param_type = models.CharField("请求入参方式", null=True, max_length=50)
    request_param = models.TextField("请求参数", null=True)
    request_body = models.TextField("body,post请求参数", null=True)
    request_data_params = models.TextField("post请求form-data入参", null=True)
    request_assert = models.TextField("请求断言", null=True)
    parameter_extraction = models.TextField("提取参数内容", null=True)

    def as_dict(self):
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            "id": self.id,
            "project_id": self.project_id,
            "env_id": self.env_id,
            "name": self.name,
            "method": self.method,
            "path": self.path,
            "domain": self.domain,
            "api_suite_id": self.api_suite_id,
            "request_header": json_loads(self.request_header),
            "request_param_type": self.request_param_type,
            "request_param": json_loads(self.request_param),
            "request_body": self.request_body,
            "request_data_params": json_loads(self.request_data_params),
            "request_assert": json_loads(self.request_assert),
            "parameter_extraction": json_loads(self.parameter_extraction),
            "create_time": create_time,
            "update_time": update_time,
            "is_deleted": self.is_deleted
        }


class ApiRequestResult(CommonInfo):
    class Meta:
        verbose_name = "接口测试结果表"
        db_table = "api_request_result"

    project_id = models.BigIntegerField("项目id", null=False)
    api_id = models.BigIntegerField("接口id", null=False)
    api_name = models.CharField("接口名称", null=False, max_length=30)
    api_test_case_id = models.BigIntegerField("用例id（可为空）", null=True)
    api_test_case_step_id = models.BigIntegerField("用例步骤id（可为空）", null=True)
    test_case_group_id = models.BigIntegerField("用例集id", null=True)
    task_log_id = models.BigIntegerField("任务日志id（可为空）", null=True)
    method = models.CharField("请求方式", null=True, max_length=20)
    url = models.CharField("请求路径", null=True, max_length=255)
    request_header = models.TextField("请求头", null=True)
    request_param_type = models.CharField("请求入参方式", null=True, max_length=50)
    request_param = models.TextField("请求参数", null=True)
    request_body = models.TextField("body,post请求参数", null=True)
    request_data_params = models.TextField("post请求form-data入参", null=True)
    result_body = models.TextField('返回结果', null=True)
    response_headers = models.TextField('返回头', null=True)
    result_status = models.IntegerField('返回状态', null=True)
    result_time = models.IntegerField('接口运行时间(ms)', null=True)
    result_assert = models.TextField('请求断言结果', null=True)
    result_is_pass = models.BooleanField('判断运行接口是否报错', null=True)
    result_extractions = models.TextField('获取提取参数结果', null=True)
    exception_body = models.TextField('报错信息', null=True)

    def as_dict(self):
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            "id": self.id,
            "project_id": self.project_id,
            "api_id": self.api_id,
            "api_name": self.api_name,
            "api_test_case_id": self.api_test_case_id,
            "test_case_group_id": self.test_case_group_id,
            "api_test_case_step_id": self.api_test_case_step_id,
            "task_log_id": self.task_log_id,
            "method": self.method,
            "url": self.url,
            "request_header": json_loads(self.request_header),
            "request_param_type": self.request_param_type,
            "request_param": self.request_param,
            "request_body": self.request_body,
            "request_data_params": json_loads(self.request_data_params),
            "result_body": self.result_body,
            "response_headers": json_loads(self.response_headers),
            "result_status": self.result_status,
            "result_time": self.result_time,
            "result_assert": json_loads(self.result_assert),
            "result_is_pass": self.result_is_pass,
            "result_extractions": json_loads(self.result_extractions),
            "exception_body": self.exception_body,
            "create_time": create_time,
            "update_time": update_time,
            "is_deleted": self.is_deleted
        }


class TestCase(CommonInfo):
    class Meta:
        verbose_name = "测试用例"
        db_table = "test_case"

    project_id = models.BigIntegerField("项目id", null=False)
    env_id = models.IntegerField("环境id", default=0)
    name = models.CharField("测试用例名称", null=False, max_length=100)
    is_skip = models.BooleanField(default=False)
    remark = models.CharField("备注", null=True, blank=True, max_length=255)


class TestCaseStep(CommonInfo):
    class Meta:
        verbose_name = "测试用例"
        db_table = "test_case_step"

    test_case_id = models.BigIntegerField("测试用例id", null=False, default=0)
    api_id = models.BigIntegerField("接口id", null=False)
    name = models.CharField("接口名称", null=False, max_length=100)
    method = models.CharField("请求方式", null=True, max_length=20)
    path = models.CharField("请求路径", null=True, blank=True, max_length=255)
    domain = models.CharField("域名", null=True, max_length=50)
    api_suite_id = models.BigIntegerField("接口分类 ID", null=False)
    request_header = models.TextField("请求头", null=True)
    request_param_type = models.CharField("请求入参方式", null=True, max_length=50)
    request_param = models.TextField("请求参数", null=True)
    request_data_params = models.TextField("post请求form-data入参", null=True)
    request_body = models.TextField("body,post请求参数", null=True)
    request_assert = models.TextField("请求断言", null=True)
    parameter_extraction = models.TextField("提取参数内容", null=True)
    sort = models.IntegerField("步骤顺序", null=False, default=0)
    is_synchronized = models.BooleanField("判断是否可同步", null=False, default=False)

    def as_dict(self):
        create_time = self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            "id": self.id,
            "test_case_id": self.test_case_id,
            "api_id": self.api_id,
            "name": self.name,
            "method": self.method,
            "path": self.path,
            "domain": self.domain,
            "api_suite_id": self.api_suite_id,
            "request_header": json_loads(self.request_header),
            "request_param_type": self.request_param_type,
            "request_param": json_loads(self.request_param),
            "request_body": self.request_body,
            "request_data_params": json_loads(self.request_data_params),
            "request_assert": json_loads(self.request_assert),
            "parameter_extraction": json_loads(self.parameter_extraction),
            "create_time": create_time,
            "update_time": update_time,
            "is_deleted": self.is_deleted
        }


class TestCaseGroup(CommonInfo):
    class Meta:
        verbose_name = "测试用例组"
        db_table = "test_case_group"

    project_id = models.BigIntegerField("项目id", null=False)
    name = models.CharField("测试用例组名称", null=False, max_length=100)
    remark = models.CharField("备注", null=True, blank=True, max_length=255)
    test_case_merge = models.ManyToManyField(TestCase)


class TestCaseResult(CommonInfo):
    class Meta:
        verbose_name = "测试用例结果表"
        db_table = "test_case_result"

    group_id = models.BigIntegerField("测试用例组id", null=True)
    test_case_id = models.BigIntegerField("测试用例id", null=False)
    test_case_name = models.CharField("测试用例名称", null=False, max_length=30)
    task_log_id = models.BigIntegerField("任务日志id", null=True)
    step_count_results = models.IntegerField("执行用例步骤总数", null=False)
    step_pass_results = models.IntegerField("执行用例步骤成功数", null=False)
    step_fail_results = models.IntegerField("执行用例步骤失败数", null=False)
    test_run_time = models.IntegerField("用例执行时间", null=False)


class TestCaseGroupResult(CommonInfo):
    class Meta:
        verbose_name = "测试用例集结果表"
        db_table = "test_case_group_result"

    test_case_ids = models.TextField("请求头", null=True)
    test_case_group_id = models.BigIntegerField("测试用例集id", null=False)
    test_case_group_name = models.CharField("测试用例名称", null=True, max_length=30)
    task_log_id = models.BigIntegerField("任务日志id", null=True)
    test_case_count_result = models.IntegerField("执行用例总数", null=False)
    test_case_pass_result = models.IntegerField("执行用例成功数", null=False)
    test_case_failed_result = models.IntegerField("执行用例失败数", null=False)
    test_case_skip_result = models.IntegerField("执行用例跳过数", null=False)


class TestCaseProcessor(CommonInfo):
    class Meta:
        verbose_name = "测试用例前置处理器表"
        db_table = "test_case_processor"

    step_id = models.BigIntegerField("测试用例步骤id", null=False)
    type = models.IntegerField("处理器内容", null=False)
    db_connection_id = models.BigIntegerField("数据库连接id", null=True)
    processor_type = models.IntegerField("处理器类型，0代表前置处理器，1代表后置处理器", null=False)
    content = models.TextField(null=True, verbose_name='前置处理器内容')
